﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKDimensionModel.SymbolTextSetting
{
	internal class IndexMarkFontSetting : ISymbolFontSetting
	{
		public Document Doc { get; set; }

		public IndexMarkCollector IndexMarkCollector { get; set; }

		public GridAndIndexMarkOper IndexMarkOper { get; set; }

		public IndexMarkFontSetting(IndexMarkCollector indexMarkCollector, GridAndIndexMarkOper indexMarkOper)
		{
			this.Doc = indexMarkCollector.Doc;
			this.IndexMarkCollector = indexMarkCollector;
			this.IndexMarkOper = indexMarkOper;
		}

		private bool IsInculdeSchema(AnnotationSymbol annotationSymbol)
		{
			if (annotationSymbol == null)
			{
				return false;
			}
			Guid guid = new Guid("4F9061E6-51F5-4FA4-B5F6-B1913E18A434");
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				return false;
			}
			Entity entity = annotationSymbol.GetEntity(schema);
			return entity != null && entity.Schema != null;
		}

		private bool CreateSchema(AnnotationSymbol annotationSymbol, ElementId annotationSymbolId)
		{
			bool result;
			try
			{
				Guid guid = new Guid("4F9061E6-51F5-4FA4-B5F6-B1913E18A434");
				Schema schema = Schema.Lookup(guid);
				if (schema == null)
				{
					SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
					schemaBuilder.SetSchemaName("AnnatationSymbolExtendName");
					schemaBuilder.AddSimpleField("annotationSymbol", typeof(ElementId));
					schema = schemaBuilder.Finish();
				}
				Entity entity = new Entity(schema);
				Field field = schema.GetField("annotationSymbol");
				entity.Set<ElementId>(field, annotationSymbolId);
				annotationSymbol.SetEntity(entity);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public bool GetFamilySymbolId(AnnotationSymbol annotationSymbol, out ElementId annotationSymbolId)
		{
			if (annotationSymbol == null)
			{
				annotationSymbolId = null;
				return false;
			}
			Guid guid = new Guid("4F9061E6-51F5-4FA4-B5F6-B1913E18A434");
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				annotationSymbolId = null;
				return false;
			}
			Entity entity = annotationSymbol.GetEntity(schema);
			if (entity == null)
			{
				annotationSymbolId = null;
				return false;
			}
			if (entity.Schema == null)
			{
				annotationSymbolId = null;
				return false;
			}
			Field field = schema.GetField("annotationSymbol");
			annotationSymbolId = entity.Get<ElementId>(field);
			return true;
		}

		public void DoSetGridAndIndexMarkStyle()
		{
			switch (this.IndexMarkOper)
			{
			case GridAndIndexMarkOper.InTheCircle:
				this.SetInTheCircle();
				return;
			case GridAndIndexMarkOper.OutTheCircle:
				this.SetOutTheCircle();
				return;
			case GridAndIndexMarkOper.OldStyle:
				this.SetOldStyle();
				return;
			default:
				return;
			}
		}

		private bool ChangeIndexMarkTextStyle(AnnotationSymbol annotationSymbol)
		{
			if (!this.IsInculdeSchema(annotationSymbol))
			{
				using (Transaction transaction = new Transaction(this.Doc))
				{
                    if (transaction.Start("Create Schema") != (TransactionStatus)1)
					{
						return false;
					}
					this.CreateSchema(annotationSymbol, annotationSymbol.Symbol.Id);
					transaction.Commit();
				}
			}
			ElementId annotationSymbol2 = null;
			bool flag = this.GetFamilySymbolId(annotationSymbol, out annotationSymbol2);
			Family indexMarkFamily = Ultility.GetIndexMarkFamily(this.Doc, annotationSymbol2);
			double num = annotationSymbol.GetParameters("圆半径").First<Parameter>().AsDouble();
			double num2 = 0.0;
			double num3 = 0.0;
			int indexMarkTextHeightAndScale = Ultility.GetIndexMarkTextHeightAndScale(indexMarkFamily, ref num2, ref num3);
			if (indexMarkTextHeightAndScale != 0)
			{
				return false;
			}
			string text = annotationSymbol.GetParameters("详图编号").First<Parameter>().AsString();
			string text2 = annotationSymbol.GetParameters("图纸编号").First<Parameter>().AsString();
			double num4 = num * 2.0 - num2 * num3 * (double)Ultility.CalTextLength(text);
			double value = num3;
			double value2 = num2;
			if (num4 < 0.0)
			{
				if (this.IndexMarkOper == GridAndIndexMarkOper.InTheCircle)
				{
					value2 = num2;
					value = Ultility.CalIndexMarkTextHeight(num, num2, (double)Ultility.CalTextLength(text), (double)this.Doc.ActiveView.Scale);
				}
				else if (this.IndexMarkOper == GridAndIndexMarkOper.OldStyle)
				{
					value = num3;
					value2 = Ultility.CalIndexMarkTextScale(num, num3, (double)Ultility.CalTextLength(text), (double)this.Doc.ActiveView.Scale);
				}
				else
				{
					value = num3;
					value2 = num2;
				}
			}
			num4 = num * 2.0 - num2 * num3 * (double)Ultility.CalTextLength(text2);
			double value3 = num3;
			double value4 = num2;
			if (num4 < 0.0)
			{
				if (this.IndexMarkOper == GridAndIndexMarkOper.InTheCircle)
				{
					value4 = num2;
					value3 = Ultility.CalIndexMarkTextHeight(num, num2, (double)Ultility.CalTextLength(text2), (double)this.Doc.ActiveView.Scale);
				}
				else if (this.IndexMarkOper == GridAndIndexMarkOper.OldStyle)
				{
					value3 = num3;
					value4 = Ultility.CalIndexMarkTextScale(num, num3, (double)Ultility.CalTextLength(text2), (double)this.Doc.ActiveView.Scale);
				}
				else
				{
					value3 = num3;
					value4 = num2;
				}
			}
			string str = string.Concat(new string[]
			{
				Math.Round(value, 5).ToString(),
				"mm-",
				Math.Round(value2, 5).ToString(),
				"-",
				Math.Round(value3, 5).ToString(),
				"mm-",
				Math.Round(value4, 5).ToString()
			});
			string annotationTypeName = "公制注释-" + str;
			string familyName = "HY-IndexMark-" + str;
			Family family = null;
			string text3 = "";
			FamilySymbol familySymbol;
			if (!Ultility.IsExistFamily(this.Doc, familyName, out family))
			{
				using (Transaction transaction2 = new Transaction(this.Doc))
				{
					if (transaction2.Start("Create Schema") != (TransactionStatus)1)
					{
						return false;
					}
					flag = this.Doc.LoadFamily(text3, out family);
					Ultility.DeleteFile(text3);
					if (!flag || family == null)
					{
						return false;
					}
					familySymbol = (this.Doc.GetElement(family.GetFamilySymbolIds().First<ElementId>()) as FamilySymbol);
					transaction2.Commit();
				}
			}
			familySymbol = (this.Doc.GetElement(family.GetFamilySymbolIds().First<ElementId>()) as FamilySymbol);
			if (familySymbol == null)
			{
				return false;
			}
			AnnotationSymbolType annotationSymbolType = null;
			using (Transaction transaction3 = new Transaction(this.Doc))
			{
				if (transaction3.Start("Change GridType And FamilySymbol") != (TransactionStatus)1)
				{
					return false;
				}
				if (!this.IsExsitSameAnnotationSymbolType(annotationTypeName, out annotationSymbolType))
				{
					annotationSymbol.ChangeTypeId(familySymbol.Id);
					annotationSymbol.GetParameters("详图编号").First<Parameter>().Set(text);
					annotationSymbol.GetParameters("图纸编号").First<Parameter>().Set(text2);
				}
				else
				{
					annotationSymbol.ChangeTypeId(annotationSymbolType.Id);
					annotationSymbol.GetParameters("详图编号").First<Parameter>().Set(text);
					annotationSymbol.GetParameters("图纸编号").First<Parameter>().Set(text2);
				}
				transaction3.Commit();
			}
			return false;
		}

		private bool ChangeIndexMarkTextStyle1(AnnotationSymbol annotationSymbol)
		{
			DetailIndexParameter detailIndexParameter = new DetailIndexParameter(annotationSymbol);
			if (!this.IsInculdeSchema(annotationSymbol))
			{
				using (Transaction transaction = new Transaction(this.Doc))
				{
                    if (transaction.Start("Create Schema") != (TransactionStatus)1)
					{
						return false;
					}
					this.CreateSchema(annotationSymbol, annotationSymbol.Symbol.Id);
					transaction.Commit();
				}
			}
			ElementId annotationSymbol2 = null;
			bool flag = this.GetFamilySymbolId(annotationSymbol, out annotationSymbol2);
			Family indexMarkFamily = Ultility.GetIndexMarkFamily(this.Doc, annotationSymbol2);
			string text = annotationSymbol.GetParameters("详图编号").First<Parameter>().AsString();
			string text2 = annotationSymbol.GetParameters("图纸编号").First<Parameter>().AsString();
			double num = annotationSymbol.GetParameters("圆半径").First<Parameter>().AsDouble();
			double num2 = 0.0;
			double num3 = 0.0;
			string text3;
			int indexMarkTextHeightAndScale;
			if (Ultility.CalTextLength(text) > Ultility.CalTextLength(text2))
			{
				text3 = text;
				indexMarkTextHeightAndScale = Ultility.GetIndexMarkTextHeightAndScale1("详图编号", indexMarkFamily, ref num2, ref num3);
			}
			else
			{
				text3 = text2;
				indexMarkTextHeightAndScale = Ultility.GetIndexMarkTextHeightAndScale1("图纸编号", indexMarkFamily, ref num2, ref num3);
			}
			if (indexMarkTextHeightAndScale != 0)
			{
				return false;
			}
			Ultility.CalTextLength(text3);
			if (!Ultility.IfTextInCircle(num, num3, num2, Ultility.CalTextLength(text3)))
			{
				return false;
			}
			double num4;
			if (this.IndexMarkOper == GridAndIndexMarkOper.InTheCircle)
			{
				num4 = Ultility.CalIndexMarkTextHeight(num, num2, (double)Ultility.CalTextLength(text3), (double)this.Doc.ActiveView.Scale);
			}
			else if (this.IndexMarkOper == GridAndIndexMarkOper.OldStyle)
			{
				num4 = num3;
				Ultility.CalIndexMarkTextScale(num, num3, (double)Ultility.CalTextLength(text3), (double)this.Doc.ActiveView.Scale);
			}
			else
			{
				num4 = num3;
			}
			string str = Math.Round(num4, 5).ToString() + "mm-" + Math.Round(num2, 5).ToString();
			string familyName = "HY-IndexMark-" + str;
			Family family;
			FamilySymbol familySymbol;
			if (!Ultility.IsExistFamily(this.Doc, familyName, out family))
			{
				string text4;
				Ultility.CreateNewFamily(indexMarkFamily, num4, num2, familyName, out text4);
				using (Transaction transaction2 = new Transaction(this.Doc))
				{
					if (transaction2.Start("Create Schema") != (TransactionStatus)1)
					{
						return false;
					}
					flag = this.Doc.LoadFamily(text4, out family);
					Ultility.DeleteFile(text4);
					if (!flag || family == null)
					{
						return false;
					}
					familySymbol = (this.Doc.GetElement(family.GetFamilySymbolIds().First<ElementId>()) as FamilySymbol);
					transaction2.Commit();
					goto IL_29D;
				}
			}
			familySymbol = (this.Doc.GetElement(family.GetFamilySymbolIds().First<ElementId>()) as FamilySymbol);
			IL_29D:
			if (!familySymbol.IsActive)
			{
				familySymbol.Activate();
			}
			using (Transaction transaction3 = new Transaction(this.Doc, "Change Type"))
			{
				transaction3.Start();
				annotationSymbol.ChangeTypeId(familySymbol.Id);
				detailIndexParameter.SetPara(annotationSymbol);
				transaction3.Commit();
			}
			return false;
		}

		private bool IsExsitSameAnnotationSymbolType(string annotationTypeName, out AnnotationSymbolType ast)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.Doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(FamilySymbol));
			IList<Element> list = filteredElementCollector.WherePasses(elementClassFilter).OfCategory(BuiltInCategory.OST_GenericAnnotation).ToElements();
			foreach (Element element in list)
			{
				AnnotationSymbolType annotationSymbolType = element as AnnotationSymbolType;
				if (annotationSymbolType != null && annotationSymbolType != null && annotationSymbolType.Name == annotationTypeName)
				{
					ast = annotationSymbolType;
					return true;
				}
			}
			ast = null;
			return false;
		}

		public bool SetInTheCircle()
		{
			foreach (AnnotationSymbol annotationSymbol in this.IndexMarkCollector.IndexMarkList)
			{
				this.ChangeIndexMarkTextStyle1(annotationSymbol);
			}
			return true;
		}

		public bool SetOldStyle()
		{
			foreach (AnnotationSymbol annotationSymbol in this.IndexMarkCollector.IndexMarkList)
			{
				this.ChangeIndexMarkTextStyle1(annotationSymbol);
			}
			return true;
		}

		public bool SetOutTheCircle()
		{
			ElementId elementId = null;
			using (Transaction transaction = new Transaction(this.Doc))
			{
                if (transaction.Start("SetOutTheCircle") == (TransactionStatus)1)
				{
					foreach (AnnotationSymbol annotationSymbol in this.IndexMarkCollector.IndexMarkList)
					{
						DetailIndexParameter detailIndexParameter = new DetailIndexParameter(annotationSymbol);
						annotationSymbol.GetParameters("详图编号").First<Parameter>().AsString();
						annotationSymbol.GetParameters("图纸编号").First<Parameter>().AsString();
						if (!this.IsInculdeSchema(annotationSymbol))
						{
							this.CreateSchema(annotationSymbol, annotationSymbol.GetTypeId());
						}
						this.GetFamilySymbolId(annotationSymbol, out elementId);
						annotationSymbol.ChangeTypeId(elementId);
						detailIndexParameter.SetPara(annotationSymbol);
					}
					transaction.Commit();
				}
			}
			return true;
		}

		private const string GRID_GUID = "4F9061E6-51F5-4FA4-B5F6-B1913E18A434";

		private const string DetailNum = "详图编号";

		private const string DrawingNum = "图纸编号";
	}
}
